package cn.codesea.housework_user.ui.manager;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnLongClick;
import cn.codesea.housework_user.R;
import cn.codesea.housework_user.entity.Account;
import cn.codesea.housework_user.entity.Activity;
import cn.codesea.housework_user.entity.User;
import cn.codesea.housework_user.net.ServiceGenerator;
import cn.codesea.housework_user.net.data.Result;
import cn.codesea.housework_user.net.service.ActivityService;
import cn.codesea.housework_user.net.service.ManagerService;
import cn.codesea.housework_user.ui.JHListActivity;
import cn.codesea.housework_user.ui.LoginActivity;
import cn.codesea.housework_user.ui.MainActivity;
import cn.codesea.housework_user.utils.SPUtil;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AcEditActivity extends AppCompatActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    @BindView(R.id.submit_tv)
    TextView submit_tv;
    @BindView(R.id.del_tv)
    TextView del_tv;
    @BindView(R.id.edit_ll)
    LinearLayout edit_ll;

    @BindView(R.id.add_tv)
    TextView add_tv;

    @BindView(R.id.actName_et)
    TextView actName_et;
    @BindView(R.id.actTime_et)
    TextView actTime_et;
    @BindView(R.id.actAddress_et)
    TextView actAddress_et;
    @BindView(R.id.actNum_et)
    TextView actNum_et;
    @BindView(R.id.actPrice_et)
    TextView actPrice_et;
    @BindView(R.id.actDesc_et)
    TextView actDesc_et;

    Activity act;
    String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ac_edit);
        ButterKnife.bind(this);
        init();
    }

    public void init(){
        initData();
        initView();
    }

    public void initData(){
        act = (Activity) getIntent().getSerializableExtra("act");
        type = getIntent().getStringExtra("type");
    }

    public void initView(){
        initToolbar();
        if (act != null){
            actName_et.setText(act.getName());
            actTime_et.setText(act.getTime());
            actAddress_et.setText(act.getAdress());
            actNum_et.setText(act.getNum()+"");
            actPrice_et.setText(act.getPrice()+"");
            actDesc_et.setText(act.getDesc());
        }
        if ("1".equals(type)){
            // 新增
            edit_ll.setVisibility(View.GONE);
            add_tv.setVisibility(View.VISIBLE);
        }
    }

    public void initToolbar(){
        // 初始化 toolbar
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick(R.id.submit_tv)
    public void submit_tv(){

        act.setName(actName_et.getText().toString());
        act.setTime(actTime_et.getText().toString());
        act.setAdress(actAddress_et.getText().toString());
        act.setNum(Integer.parseInt(actNum_et.getText().toString()));
        act.setPrice(Double.parseDouble(actPrice_et.getText().toString()));
        act.setDesc(actDesc_et.getText().toString());

        ActivityService service = ServiceGenerator.createService(ActivityService.class);
        Call<Result> call = service.activityUpdate(act);
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body(),"更新");
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                Toast.makeText(AcEditActivity.this,"网络错误!",Toast.LENGTH_SHORT).show();
            }
        });

    }

    @OnClick(R.id.del_tv)
    public void del_tv(){

        ActivityService service = ServiceGenerator.createService(ActivityService.class);
        Call<Result> call = service.actDel(act.getActivityid());
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body(),"删除");
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                Toast.makeText(AcEditActivity.this,"网络错误!",Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick(R.id.add_tv)
    public void add_tv(){

        act = new Activity();
        act.setName(actName_et.getText().toString());
        act.setTime(actTime_et.getText().toString());
        act.setAdress(actAddress_et.getText().toString());
        act.setNum(Integer.parseInt(actNum_et.getText().toString()));
        act.setPrice(Double.parseDouble(actPrice_et.getText().toString()));
        act.setDesc(actDesc_et.getText().toString());
        act.setStatus(0);

        ActivityService service = ServiceGenerator.createService(ActivityService.class);
        Call<Result> call = service.activityAdd(act);
        call.enqueue(new Callback<Result>(){
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                resultProcess(response.body(),"新增");
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                Toast.makeText(AcEditActivity.this,"网络错误!",Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 结果处理
     */
    public void resultProcess(Result result,String msg){
        if (result.getState() == 200){
            Dialog dialog = new AlertDialog.Builder(AcEditActivity.this)
                    .setTitle("提示")
                    .setMessage(msg + "成功！")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            startActivity(new Intent(AcEditActivity.this,ManagerMainActivity.class));
                        }
                    })
                    .create();
            dialog.setCancelable(false);
            dialog.show();
        }else {
            Dialog dialog = new AlertDialog.Builder(AcEditActivity.this)
                    .setTitle("提示")
                    .setMessage(msg + "失败！")
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    })
                    .create();
            dialog.setCancelable(false);
            dialog.show();
        }
    }
}
